package com.mytest.offer;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SolutionJZ3 {

    public static void main(String[] args) {
        SolutionJZ3 solutionJZ6 = new SolutionJZ3();
        int[] numbers = {2,3,1,0,2,5,3};

        log.info("{}", solutionJZ6.duplicate(numbers));
    }
    public int duplicate (int[] numbers) {
        // write code here

        int length = numbers.length;
        for (int i = 0;i< length;i++){
            if (numbers[i] == i){
                continue;
            }else{
                if (numbers[i] == numbers[numbers[i]]){
                    return numbers[i];
                } else{
                    //交换
                    swap(numbers, i, numbers[i]);
                }
            }
        }
        return -1;
    }

    private void swap(int[] numbers, int a, int b){
        int temp = numbers[a];
        numbers[a] = numbers[b];
        numbers[b] = temp;
    }
}
